package com.chw.controller;

import com.chw.common.ChwTS;
import com.chw.model.Key;
import com.chw.model.Type;
import com.chw.model.vo.UserVo;
import com.chw.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("type/")
public class TypeController {
    @Autowired
    private TypeService typeService;
    //查询
    @RequestMapping("queryType")
    public ChwTS queryType(Type type){
        try{
           return ChwTS.success(typeService.queryType());
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //新增
    @RequestMapping("addType")
    public ChwTS addType(Type type){
        try{
            if (type.getPid()==null){
                return ChwTS.error(400,"缺少节点参数");
            }
            typeService.addType(type);
            return ChwTS.success("");
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //修改
    @RequestMapping("updateType")
    public ChwTS updateType(Type type){
        try{
            if (type.getId()==null){
                return ChwTS.error(400,"缺少参数");
            }
            typeService.updateType(type);
            return ChwTS.success("");
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //逻辑删除
    @RequestMapping("deleteType")
    public ChwTS deleteType(Integer id){
        try{
            if (id==null){
                return ChwTS.error(400,"缺少参数");
            }
            typeService.deleteType(id);
            return ChwTS.success("");
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //查询key
    @RequestMapping("queryKey")
    public ChwTS queryKey(UserVo userVo){
        try{


            return ChwTS.success(typeService.queryKey(userVo));
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //修改
    @RequestMapping("updateKey")
    public ChwTS updateKey(Key key){
        try{
            if (key.getId()==null){
                return ChwTS.error(400,"缺少参数");
            }
            typeService.updateKey(key);
            return ChwTS.success("");
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    //key的新增
    @RequestMapping("addKey")
    public ChwTS addKey(Key key){
        try{

            typeService.addKey(key);
            return ChwTS.success("");
        }
        catch (Exception e){
            return  ChwTS.error(500,e.getMessage());
        }
    }
    @RequestMapping("queryChildrenStr")
    public ChwTS  queryChildrenStr(){
        return ChwTS.success(typeService.queryChildrenStr());
    }
    //对于type表的数据处理返回:
    /*
*
* [
                  {"id":4,"name":"飞狐电商/电子产品/电脑"},
                  {"id":5,"name":"飞狐电商/电子产品/手机"},
                  {"id":6,"name":"飞狐电商/服装/外套"},
                  {"id":7,"name":"飞狐电商/电子产品/手环"},
                  {"id":8,"name":"飞狐电商/服装/上衣"},
                  {"id":9,"name":"飞狐电商/服装/鞋"}]
*
* */


    //查询key表和value
    @RequestMapping("queryKeyValue")
    public ChwTS queryKeyValue(Integer id){
        return ChwTS.success(typeService.queryKeyValue(id));
    }
}
